package com.xishu.util;

import com.xishu.config.Config;
import com.xishu.entity.AbstractEntity;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.util.*;

public interface Tools {
    public static final String STRING_TYPE = "java.lang.String";
    public static final String DOUBLE_TYPE = "java.lang.Double";
    public static final String LONG_TYPE = "java.lang.Long";
    public static final String INTEGER_TYPE = "java.lang.Integer";
    public static final String SEPARATOR_KEY = ":";


    default public boolean getBoolean(Boolean b) {
        if (b == null) {
            return false;
        }

        return b;
    }

    default public boolean isNotEmpty(Object o) {
        return o != null;
    }

    default public boolean isNotEmpty(Map map) {
        return map != null && map.size() != 0;
    }

    default public boolean isEmpty(Map map) {
        return MapUtils.isEmpty(map);
    }

    default public boolean isEmpty(Object[] array) {
        return array == null || array.length == 0;
    }

    default public boolean isNotEmpty(Object[] array) {
        return !isEmpty(array);
    }

    default public boolean isEmpty(Collection input) {
        return CollectionUtils.isEmpty(input);
    }

    default public boolean isNotEmpty(Collection input) {
        return !isEmpty(input);
    }

    default public boolean isEmpty(String string) {
        return StringUtils.isEmpty(string);
    }

    default public boolean isNotEmpty(String string) {
        return StringUtils.isNotEmpty(string);
    }

    default public Integer getInt(Integer integer) {
        if (integer == null) {
            return 0;
        }

        return integer;
    }

    default public Long getLong(Long l) {
        if (l == null) {
            return 0l;
        }

        return l;
    }

    default public Double getDouble(Double d) {
        if (d == null) {
            return 0d;
        }

        return d;
    }

    default public double showDouble(Double number, int scale) {
        if (number == null) {
            return 0d;
        }
        BigDecimal b = BigDecimal.valueOf(number);
        return b.setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }


    default public double showDouble(Double number) {
        if (number == null) {
            return 0d;
        }
        BigDecimal b = BigDecimal.valueOf(number);
        return b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    default public Double showMoneyDouble(Double number) {
        if (number == null) {
            return 0d;
        }

        //先用两位，然后再4舍5入到1位，防止
        //3.3499999变成了3.3,更应该变成3.4
//        number = showDouble(number);

        //如果是美国版本，那么使用小数点后两数
        if (Config.getInstance().isUsVersion()) {
            BigDecimal b = BigDecimal.valueOf(number);
            return b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        } else {
            BigDecimal b = BigDecimal.valueOf(number);
            return b.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
        }
    }

    /**
     * 计算小费
     *
     * @param number
     * @return
     */
    default public Double showTips(Double number) {
        if (number == null) {
            return 0d;
        }

        BigDecimal b = BigDecimal.valueOf(number);
        return b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * 小数取整
     *
     * @param number
     * @return
     */
    default public Double showInteger(Double number) {
        if (number == null) {
            return 0d;
        }

        //先用两位，然后再4舍5入到1位，防止
        //3.3499999变成了3.3,更应该变成3.4
//        number = showDouble(number);

        //美国版本不用取整，取小数点后两位
        if (Config.getInstance().isUsVersion()) {
            BigDecimal b = BigDecimal.valueOf(number);
            return b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        }


        BigDecimal b = BigDecimal.valueOf(number);
        return b.setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    default public Double formatDouble(Double d) {
        Double doubleValue = getDouble(d);
        return showDouble(doubleValue);
    }

    default public Double formatMoneyDouble(Double d) {
        Double doubleValue = getDouble(d);
        return showMoneyDouble(doubleValue);
    }

    default boolean equals(Object o1, Object o2) {
        if (o1 == o2) return true;

        if (o1 == null) {
            return false;
        }

        if (o2 == null) {
            return false;
        }

        if (o1.getClass() != o2.getClass()) return false;
        return Objects.equals(o1, o2);
    }

    default boolean equals(String str1, String str2) {
        return StringUtils.equals(str1, str2);
    }

    default boolean equalsIgnoreCase(String str1, String str2) {
        return StringUtils.equalsIgnoreCase(str1, str2);
    }

    default <T extends AbstractEntity> String getEntityKey(T t) {
        return t.getClass().getSimpleName() + "." + t.getId();
    }

    default String getEntityKey(Long id, Class tclass) {
        return tclass.getSimpleName() + "." + id;
    }

    default String addTwoKey(Object str1, Object str2) {
        return str1.toString() + "." + str2.toString();
    }

    /**
     * 需要保证不为空，以避免空指针问题
     *
     * @param list
     * @param <T>
     * @return
     */
    default <T> List<T> getList(List<T> list) {
        if (list == null) {
            return new ArrayList<>();
        }

        return list;
    }

    /**
     * 格式化输出
     *
     * @param str1
     * @return
     */
    default String getString(String str1) {
        if (str1 == null) {
            return "";
        }

        return str1;
    }

    /**
     * 日志格式化
     *
     * @param list
     * @return
     */
    default String logString(Collection list) {
        return StringUtils.join(list, ",");
    }

    /**
     * 将数组转化成按位的数字
     *
     * @param list
     * @return
     */
    default long intListToBitNumber(List<Integer> list) {
        if (isEmpty(list)) {
            return 0;
        }

        long result = 0;

        for (Integer number : list) {
            result = result + (long) (Math.pow(2, number - 1));
        }

        return result;
    }

    /**
     * 将一个二进制数的，按位求得对应的集合数组
     * 在第几位上对应就是数字几
     *
     * @param intNumber
     * @return
     */
    default List<Integer> bitNumberToIntList(int intNumber) {
        List<Integer> numberList = new ArrayList<>();
        int number = 1;
        int moveIndex = 0;

        while ((number << moveIndex) <= intNumber) {
            if (((number << moveIndex) & intNumber) == (number << moveIndex)) {
                numberList.add(moveIndex + 1);
            }

            moveIndex++;
        }

        return numberList;
    }

    /**
     * 符号数转long
     * <p>
     * 用于支付的时候转化成数字
     *
     * @param d
     * @return
     */
    default long doubleToLong(Double d) {
        Double aDouble = getDouble(d);
        return Double.valueOf(aDouble * 100).longValue();
    }

}
